# _*_coding:utf-8_*_
# Common functions and communication functions
import os
import sys

from ATS.communication import pyvisaImpl
from ATS.communication import pyvisaFake
from ATS.hardwarescan import conf_file_manager

workpath = os.path.dirname(os.path.realpath(sys.argv[0]))
sys.path.insert(0, os.path.join(os.path.dirname(workpath), 'instrumentDriver'))


def getaddress(name):
    inst_dict = conf_file_manager.get_inst_dict()
    obj = inst_dict.get(name)
    return obj.getaddress()


def getinstattr(name, attr_id):
    inst_dict = conf_file_manager.get_inst_dict()
    obj = inst_dict.get(name)
    return obj.getinfo(attr_id)


def setinstsetting(name, setting):
    obj = getinstobj(name)
    obj.setinstsetting(setting)


def getinstsetting(name, key_list):
    obj = getinstobj(name)
    return obj.getinstsetting(key_list)


def getinstobj(name):
    inst_dict = conf_file_manager.get_inst_dict()
    return inst_dict.get(name)


# for communication

def getinsthandler(inst_infodict):
    visa_impl = None
    interface_type = inst_infodict.get("interface_type")
    # print("getinsthandler", inst_infodict)
    if interface_type == "FAKE":
        visa_impl = pyvisaFake.PyvisaFake()
    if interface_type == "LAN":
        # print("inst_infodict",inst_infodict)
        visa_impl = pyvisaImpl.PyvisaImpl('LAN', lan_addr=inst_infodict["lan_addr"])
    #        id_str = "TCPIP0::%s::inst0::INSTR"%inst_infodict["lan_addr"]
    if interface_type == "GPIB":
        visa_impl = pyvisaImpl.PyvisaImpl('GPIB', addr=inst_infodict["gpib_addr"],
                                          card="GPIB%d" % inst_infodict["gpib_cardnum"])
    if interface_type == "DLL":
        # 此处是调用动态链接库的判断
        visa_impl = None

    if interface_type == "USB":
        visa_impl = pyvisaImpl.PyvisaImpl('USB', usb_addr=inst_infodict["usb_addr"])
    handler = visa_impl
    return handler


def write(name, cmd):
    inst_dict = conf_file_manager.get_inst_dict()
    obj = inst_dict.get(name)
    if obj:
        if obj.handler:
            return obj.handler.write(cmd)
        else:
            return None


def read(name):
    inst_dict = conf_file_manager.get_inst_dict()
    obj = inst_dict.get(name)
    if obj:
        if obj.handler:
            return obj.handler.read()
        else:
            return None


def ask(name, cmd):
    inst_dict = conf_file_manager.get_inst_dict()
    obj = inst_dict.get(name)
    if obj:
        if obj.handler:
            return obj.handler.ask(cmd)
        else:
            return "FAKE"


def ask_for_values(name, cmd, format=None):
    inst_dict = conf_file_manager.get_inst_dict()
    obj = inst_dict.get(name)
    if obj:
        if obj.handler:
            return obj.handler.ask_for_values(cmd, format)
        else:
            return None


def read_values(name, format=None):
    inst_dict = conf_file_manager.get_inst_dict()
    obj = inst_dict.get(name)
    if obj:
        if obj.handler:
            return obj.handler.read_values(format)
        else:
            return None


def spoll(name):
    inst_dict = conf_file_manager.get_inst_dict()
    obj = inst_dict.get(name)
    if obj:
        if obj.handler:
            return obj.handler.spoll()
        else:
            return None
